import 'package:ch_router/ch_router.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lmreader/common/routers/index.dart';

class HomePage extends ConsumerStatefulWidget {
  const HomePage({super.key});

  @override
  ConsumerState<ConsumerStatefulWidget> createState() => _HomePageState();
}

class _HomePageState extends ConsumerState<HomePage>
    with WidgetsBindingObserver {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.resumed) {
      // 应用进入前台
      print("应用进入前台");
      // assume the time out 60s
      CHNavigator.push(RouteNames.splash);
    } else if (state == AppLifecycleState.paused) {
      // 应用进入后台
      print("应用进入后台");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("HomePage")),
      body: SafeArea(
        child: _buildView(),
      ),
    );
  }

  /// 主视图
  Widget _buildView() {
    return Center(
      child: GestureDetector(
        child: const Text("HomePage"),
        onTap: () => CHNavigator.push(RouteNames.about),
      ),
    );
  }
}
